{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Convert a Number to Hexadecimal"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #bit-manipulation #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #位运算 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: toHex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #数字转换为十六进制数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一个整数，编写一个算法将这个数转换为十六进制数。对于负整数，我们通常使用&nbsp;<a href=\"https://baike.baidu.com/item/%E8%A1%A5%E7%A0%81/6854613?fr=aladdin\">补码运算</a>&nbsp;方法。</p>\n",
    "\n",
    "<p><strong>注意:</strong></p>\n",
    "\n",
    "<ol>\n",
    "\t<li>十六进制中所有字母(<code>a-f</code>)都必须是小写。</li>\n",
    "\t<li>十六进制字符串中不能包含多余的前导零。如果要转化的数为0，那么以单个字符<code>&#39;0&#39;</code>来表示；对于其他情况，十六进制字符串中的第一个字符将不会是0字符。&nbsp;</li>\n",
    "\t<li>给定的数确保在32位有符号整数范围内。</li>\n",
    "\t<li><strong>不能使用任何由库提供的将数字直接转换或格式化为十六进制的方法。</strong></li>\n",
    "</ol>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "输入:\n",
    "26\n",
    "\n",
    "输出:\n",
    "&quot;1a&quot;\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "输入:\n",
    "-1\n",
    "\n",
    "输出:\n",
    "&quot;ffffffff&quot;\n",
    "</pre>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [convert-a-number-to-hexadecimal](https://leetcode.cn/problems/convert-a-number-to-hexadecimal/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [convert-a-number-to-hexadecimal](https://leetcode.cn/problems/convert-a-number-to-hexadecimal/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['26', '-1']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num<0:\n",
    "            num = (1<<32)+ num\n",
    "        return format(num,'0x')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        dic = '0123456789abcdef'\n",
    "        num_16 = ''\n",
    "        if num < 0:\n",
    "            num += 4294967296\n",
    "        while num >= 16:\n",
    "            num_16 += dic[num % 16]\n",
    "            num //= 16\n",
    "        num_16 += dic[num]\n",
    "        return num_16[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "CONV = \"0123456789abcdef\"\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = []\n",
    "        # 32位2进制数，转换成16进制 -> 4个一组，一共八组\n",
    "        for _ in range(8):\n",
    "            ans.append(num%16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(CONV[n] for n in ans[::-1])\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num < 0:\n",
    "            num &= 0xffffffff\n",
    "        elif num == 0:\n",
    "            return \"0\"\n",
    "\n",
    "        ans = []\n",
    "        while num > 0:\n",
    "            mod = num % 16\n",
    "            if mod > 9:\n",
    "                tag = chr(mod-10+ord('a'))\n",
    "            else:\n",
    "                tag=str(mod)\n",
    "            ans.append(tag)\n",
    "            num = num // 16\n",
    "        return ''.join(ans[::-1]) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ansList = []\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 0:\n",
    "            num += 2**32\n",
    "        l = '0123456789abcdef'\n",
    "        while num>0:\n",
    "            ansList.append(l[num%16])\n",
    "            num //= 16\n",
    "        return ''.join(ansList[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        CONV = \"0123456789abcdef\"\n",
    "        ans = []\n",
    "        # 32位2进制数，转换成16进制 -> 4个一组，一共八组\n",
    "        for _ in range(8):\n",
    "            ans.append(num%16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(CONV[n] for n in ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num < 0:\n",
    "            num += (1 << 32)\n",
    "        return format(num, '0x')\n",
    "        \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = ''\n",
    "        if num < 0:\n",
    "            num = 2 ** 32 + num\n",
    "        if num == 0:return \"0\"\n",
    "        mp = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','0']\n",
    "        while num :\n",
    "            ans += mp[num % 16]\n",
    "            num //= 16\n",
    "\n",
    "        return ans[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        s_list = list(\"0123456789abcdef\")\n",
    "        res = []\n",
    "        # 2^32------->16^8\n",
    "        for _ in range(8):\n",
    "            res.append(num % 16)\n",
    "            num = num // 16\n",
    "\n",
    "            if num == 0:\n",
    "                break  \n",
    "        return \"\".join(s_list[n]  for n in res[::-1])               "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        a='0123456789abcdef'\n",
    "        c=''\n",
    "        if num<0:\n",
    "            num+=4294967296\n",
    "        while num>15:\n",
    "            c=c+a[num%16]\n",
    "            num//=16\n",
    "        return a[num] +c[::-1]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        #如果是0直接返回\"0\"\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        #十六进制字符串\n",
    "        s = \"0123456789abcdef\"\n",
    "        result = []\n",
    "        #因为是32位机器，所以是8个F\n",
    "        num = num & 0xFFFFFFFF\n",
    "        while num > 0:\n",
    "            #从右至左，每4个bit位进行与操作，得到该十六进制的字符表示\n",
    "            result.append(s[num & 0XF])\n",
    "            #每次右移4位\n",
    "            num = num >> 4\n",
    "        #结果取反序返回\n",
    "        return \"\".join(result[::-1])\n",
    "\n",
    "                    \n",
    "        \n",
    "                    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num<0:\n",
    "            num+=2**32\n",
    "        if num==0:\n",
    "            return '0'\n",
    "        s=''\n",
    "        while num>0:\n",
    "            x=num%16\n",
    "            if x<10:\n",
    "                s=str(x)+s\n",
    "            else:\n",
    "                s=chr(87+x)+s\n",
    "            num//=16\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0: return \"0\"\n",
    "        d = {10: \"a\", 11: \"b\", 12: \"c\", 13: \"d\", 14: \"e\", 15: \"f\"}\n",
    "        s = \"\"\n",
    "        total = 0\n",
    "        cnt = 0\n",
    "        for i in range(32):\n",
    "            if i % 4 == 0 and i != 0:\n",
    "                if total in d:\n",
    "                    s = str(d[total]) + s\n",
    "                else:\n",
    "                    s = str(total) + s\n",
    "                total = 0\n",
    "                cnt = 0\n",
    "            tmp = num & 1\n",
    "            total += tmp * pow(2, cnt)\n",
    "            num >>= 1\n",
    "            cnt += 1\n",
    "        if total in d:\n",
    "            s = str(d[total]) + s\n",
    "        else:\n",
    "            s = str(total) + s\n",
    "        n = len(s)\n",
    "        ans = \"\"\n",
    "        flag = True\n",
    "        for i in range(n):\n",
    "            if flag:\n",
    "                if s[i] != \"0\":\n",
    "                    ans += s[i]\n",
    "                    flag = False\n",
    "            else:\n",
    "                ans += s[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num>=0:\n",
    "            return hex(num)[2:]\n",
    "        else:\n",
    "            return hex(pow(16,8)+num)[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        o=\"0123456789abcdef\"\n",
    "        s=\"\"\n",
    "        l=[]\n",
    "        if num<0:\n",
    "            num=num+4294967296\n",
    "        while num>=16:\n",
    "            l.append(o[num%16])\n",
    "            num=num//16\n",
    "        l.append(o[num])\n",
    "        l.reverse()\n",
    "        return \"\".join(l)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        alphabet=\"0123456789abcdef\"\n",
    "        l=[]\n",
    "        if num<0:\n",
    "            num+=4294967296\n",
    "        while num>=16:\n",
    "            l.append(alphabet[num%16])\n",
    "            num=num//16\n",
    "        l.append(alphabet[num])\n",
    "        l.reverse()\n",
    "        return \"\".join(l)        \n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        # if num<0:\n",
    "        #     num+=4294967296\n",
    "        # alphabet=\"0123456789abcdef\"\n",
    "        # a=0\n",
    "        # res=\"\"\n",
    "        # numF=num\n",
    "        # while numF>0:\n",
    "        #     numF=numF%16\n",
    "        #     a+=1\n",
    "\n",
    "        # while a!=0:\n",
    "        #     b=16^(a-1)\n",
    "        #     c=(num-num%b)/b\n",
    "        #     res.join(alphabet[c])\n",
    "        #     num=num-b\n",
    "        #     a-=1  \n",
    "\n",
    "        # return res     \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "CONV = \"0123456789abcdef\"\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        res = []\n",
    "        \n",
    "        for _ in range(8):\n",
    "            res.append(num % 16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "            \n",
    "        return ''.join(CONV[n] for n in res[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return '0'\n",
    "        if num<0:\n",
    "            num = (abs(num)^(2**32-1))+1\n",
    "        return hex(num)[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "CONV = \"0123456789abcdef\"\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = []\n",
    "        # 32位2进制数，转换成16进制 -> 4个一组，一共八组\n",
    "        for _ in range(8):\n",
    "            ans.append(num%16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(CONV[n] for n in ans[::-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num<0:\n",
    "            num += 1<<32\n",
    "        return hex(num).split('x')[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        s = [str(x) for x in range(10)] + [x for x in \"abcdef\"]\n",
    "        result = []\n",
    "        if num<0:\n",
    "            num = pow(2,32) + num\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        while (num!=0):\n",
    "            yu = num%16\n",
    "\n",
    "            result.append(s[yu])\n",
    "            num = num//16\n",
    "        return ''.join(result[::-1])\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        o=\"0123456789abcdef\"\n",
    "        s=\"\"\n",
    "        l=[]\n",
    "        if num<0:\n",
    "            num=num+4294967296\n",
    "        while num>=16:\n",
    "            l.append(o[num%16])\n",
    "            num=num//16\n",
    "        l.append(o[num])\n",
    "        l.reverse()\n",
    "        return \"\".join(l)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        s = '0123456789abcdef'\n",
    "        num &= 0xFFFFFFFF\n",
    "        res = ''\n",
    "        while num:\n",
    "            res += s[num % 16]\n",
    "            num >>= 4\n",
    "        \n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        str=\"0123456789abcdef\"\n",
    "        ans=[]\n",
    "        count=0\n",
    "        for _ in range (8):\n",
    "            ans.append(str[num%16])\n",
    "            num//=16\n",
    "            if num==0:\n",
    "                break\n",
    "        return \"\".join(reversed(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        dic = {0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'a', 11:'b', 12:'c', 13:'d', 14:'e', 15:'f'}\n",
    "        res = ''\n",
    "        if num < 0:\n",
    "            num = 4294967296+num\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 16:\n",
    "            return dic[num]\n",
    "        while num > 0:\n",
    "            res = dic[num % 16] + res\n",
    "            num = num // 16\n",
    "        return res\n",
    "                    \n",
    "        \n",
    "                    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0: return \"0\"\n",
    "        mp = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','0']\n",
    "        carry = 1\n",
    "        flag = num > 0\n",
    "        num = abs(num)\n",
    "        ans = \"\"\n",
    "        while num:\n",
    "            if flag: ans += mp[num % 16]\n",
    "            else: \n",
    "                k = 15 - num % 16 + carry # 16\n",
    "                ans += mp[k]\n",
    "                if k < 16: carry = 0\n",
    "            num //= 16\n",
    "        # print(ans)\n",
    "        while not flag and len(ans) < 8:\n",
    "            ans += 'f'\n",
    "        return ans[::-1]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        return hex(num)[2:] if num>=0 else hex(num&0xffffffff)[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "CONV = \"0123456789abcdef\"\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = []\n",
    "        # 32位2进制数，转换成16进制 -> 4个一组，一共八组\n",
    "        for _ in range(8):\n",
    "            ans.append(num%16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(CONV[n] for n in ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    index = \"\".join([str(i) for i in range(10)])+ \"abcdef\"\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num < 0:\n",
    "            return self.toHex((~(-num) + 1) & 0xffffffff) \n",
    "        if num < 16:\n",
    "            return self.index[num % 16]\n",
    "        return self.toHex(num // 16) + self.index[num % 16]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        s = '0123456789abcdef'\n",
    "        num &= 0xFFFFFFFF\n",
    "        res = ''\n",
    "        while num:\n",
    "            res += s[num % 16]\n",
    "            num >>= 4\n",
    "        \n",
    "        return res[::-1]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        ans = ''\n",
    "        table = {x: str(x) for x in range(10)}\n",
    "        table = {**table, 10: \"a\", 11: \"b\", 12: \"c\", 13: \"d\", 14: \"e\", 15: \"f\"}\n",
    "        if num < 0:\n",
    "            num = 2 ** 32 + num\n",
    "        while num > 0:\n",
    "            left = num % 16\n",
    "            ans = table[left] + ans\n",
    "            num = num // 16\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        o=\"0123456789abcdef\"\n",
    "        s=\"\"\n",
    "        l=[]\n",
    "        if num<0:\n",
    "            num=num+4294967296\n",
    "        while num>=16:\n",
    "            l.append(o[num%16])\n",
    "            num=num//16\n",
    "        l.append(o[num])\n",
    "        l.reverse()\n",
    "        return \"\".join(l)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "d = {\"0\":\"f\", \"1\":\"e\", \"2\":'d', '3':'c', '4':'b', '5':'a', '6':'9', '7':\"8\", '8':'7', '9':'6', 'a':'5', 'b':\"4\", 'c':'3', 'd':'2', 'e':'1', 'f':'0'}\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        st = []\n",
    "        flag = False\n",
    "        if num < 0:\n",
    "            num = -num\n",
    "            flag = True\n",
    "        while num > 0:\n",
    "            st.append(str(num % 16) if num % 16 <= 9 else chr(ord('a') + num % 16 - 10))\n",
    "            num //= 16\n",
    "        st = st[::-1] \n",
    "        if flag:\n",
    "            if len(st) < 8:\n",
    "                st = [\"0\"] * (8-len(st)) + st\n",
    "            # 对st取反+1\n",
    "            for i, x in enumerate(st):\n",
    "                st[i] = d[x]\n",
    "            def f(st, i):\n",
    "                if st[i] != 'f' and st[i] != '9':\n",
    "                    st[i] = chr(ord(st[i])+1)\n",
    "                elif st[i] == '9':\n",
    "                    st[i] = 'a'\n",
    "                else:\n",
    "                    st[i] = '0'\n",
    "                    f(st, i-1)\n",
    "            f(st, len(st)-1)\n",
    "        return ''.join(st)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        dic = {0:'0', 1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', 10:'a', 11:'b', 12:'c', 13:'d', 14:'e', 15:'f'}\n",
    "        res = ''\n",
    "        if num < 0:\n",
    "            num = 4294967296+num\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 16:\n",
    "            return dic[num]\n",
    "        while num > 0:\n",
    "            res = dic[num % 16] + res\n",
    "            num = num // 16\n",
    "        return res\n",
    "                    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        hexlist = \"0123456789abcdef\"\n",
    "        ans = []\n",
    "        for _ in range(8):\n",
    "            ans.append(num % 16)\n",
    "            num = num // 16\n",
    "            # 如果为0，结束循环\n",
    "            if not num:\n",
    "                break\n",
    "        res = \"\"\n",
    "        for i in range(len(ans)-1, -1, -1):\n",
    "            print(ans[i])\n",
    "            res += hexlist[ans[i]]\n",
    "        return res\n",
    "\n",
    "        # Flag = False\n",
    "        # if num < 0:\n",
    "        #     Flag = True\n",
    "        # # res = \"\"\n",
    "        # indexlist = []\n",
    "        # while num > 0:\n",
    "        #     tmp = hexlist[num % 16]\n",
    "        #     # res = tmp + res\n",
    "        #     indexlist = [tmp] + indexlist\n",
    "        #     num = num // 16\n",
    "\n",
    "        # if Flag:\n",
    "        #     # 16进制，最多8位\n",
    "        #     for i in range(8):\n",
    "        #         res[i] = 15 - res[i]\n",
    "\n",
    "        #     carry = 1\n",
    "        #     n = 8 - 1\n",
    "        #     while carry == 1:\n",
    "        #         res[n] += 1\n",
    "        #         if res[n] < 15:\n",
    "        #             carry = 0\n",
    "        #             break\n",
    "        #         else:\n",
    "        #             res[n] = res[n] % 15\n",
    "        # res = \"\"\n",
    "        # for num in indexlist:\n",
    "        #     res = hexlist[num] + res\n",
    "        # return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "\n",
    "        x = 0xffffffff\n",
    "        num = num & x \n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        hex_ch = [\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"]\n",
    "\n",
    "        ans =\"\"\n",
    "        while num>0:\n",
    "            y = num%16\n",
    "            ans += hex_ch[y]\n",
    "            num >>= 4\n",
    "        return ans[::-1]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num<0:\n",
    "            num = (1<<32)+num\n",
    "        return format(num, '0x')\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        CONV = \"0123456789abcdef\"\n",
    "        ans = []\n",
    "        for _ in range(8):\n",
    "            ans.append(CONV[num % 16])\n",
    "            num = num // 16\n",
    "\n",
    "            if not num:\n",
    "                break\n",
    "        \n",
    "        return ''.join(ans[::-1])\n",
    "        # ans.reverse()\n",
    "        # return ''.join(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        temp = '0123456789abcdef'\n",
    "        res = ''\n",
    "        if num < 0:\n",
    "            num = 4294967296+num\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 16:\n",
    "            return temp[num]\n",
    "        while num > 0:\n",
    "            res = temp[num % 16] + res\n",
    "            num = num // 16\n",
    "        return res\n",
    "                    \n",
    "        \n",
    "\n",
    "                    \n",
    "        \n",
    "                    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        hexdigits = '0123456789abcdef'\n",
    "        if num < 0:\n",
    "            num += 2 ** 32\n",
    "        ans = ''\n",
    "        while num:\n",
    "            ans += hexdigits[num % 16]\n",
    "            num //= 16\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        o=\"0123456789abcdef\"\n",
    "        s=\"\"\n",
    "        l=[]\n",
    "        if num<0:\n",
    "            num=num+4294967296\n",
    "        while num>=16:\n",
    "            l.append(o[num%16])\n",
    "            num=num//16\n",
    "        l.append(o[num])\n",
    "        l.reverse()\n",
    "        return \"\".join(l)\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def toHex(self, num: int) -> str:\r\n",
    "        if num < 0:\r\n",
    "            return hex((1<<32)+num)[2:]\r\n",
    "        return hex(num)[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        o=\"0123456789abcdef\"\n",
    "        s=\"\"\n",
    "        l=[]\n",
    "        if num<0:\n",
    "            num=num+4294967296\n",
    "        while num>=16:\n",
    "            l.append(o[num%16])\n",
    "            num=num//16\n",
    "        l.append(o[num])\n",
    "        l.reverse()\n",
    "        return \"\".join(l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 0:\n",
    "            num = 2**32 + num\n",
    "        \n",
    "        ans = ''\n",
    "        hmap = '0123456789abcdef'\n",
    "        while num:\n",
    "            ans = hmap[num % 16] + ans\n",
    "            num = num // 16\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        conv = \"0123456789abcdef\"\n",
    "        ans = []\n",
    "        for i in range(8):\n",
    "            ans.append(num%16)\n",
    "            num //= 16\n",
    "            print(num)\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(conv[i] for i in ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "CONV = \"0123456789abcdef\"\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = []\n",
    "        # 32位2进制数，转换成16进制 -> 4个一组，一共八组\n",
    "        for _ in range(8):\n",
    "            ans.append(num%16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(CONV[n] for n in ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        indmap = ['0','1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f']\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        isnegative = False\n",
    "        if num < 0:\n",
    "            isnegative = True\n",
    "            num = -num\n",
    "        result = []\n",
    "        while num > 0:\n",
    "            result.insert(0, indmap[num & 0xf])\n",
    "            num = num >> 4\n",
    "        if isnegative:\n",
    "            new_result = []\n",
    "            add = 1\n",
    "            result = [\"0\"]*(8-len(result)) + result\n",
    "            for a in result[::-1]:\n",
    "                new_result.insert(0, indmap[(15+add-indmap.index(a))%16])\n",
    "                if 15+add-indmap.index(a) > 15:\n",
    "                    add = 1\n",
    "                else:\n",
    "                    add = 0\n",
    "            return \"\".join(new_result)\n",
    "        else:\n",
    "            return \"\".join(result)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num==0:\n",
    "            return \"0\"\n",
    "        b=\"0123456789abcdef\"\n",
    "        s=\"\"\n",
    "        num=num&0xffffffff\n",
    "        while num:\n",
    "            n=num%16\n",
    "            s=b[n]+s\n",
    "            num//=16\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "CONV = \"0123456789abcdef\"\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "     \n",
    "        ans = []\n",
    "        for _ in range (8):\n",
    "            ans.append(num%16)\n",
    "            num //=16\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(CONV[n] for n in ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        \n",
    "        res = ''\n",
    "        h = [str(i) for i in range(10)] + [chr(97+i) for i in range(6)]\n",
    "        count = 0\n",
    "        while num != 0 and count < 8:\n",
    "            res = h[num&0xf] + res\n",
    "            num >>= 4\n",
    "            count += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num>=0:\n",
    "            return format(num,'x')\n",
    "        if num<=0:\n",
    "            a = num+4294967296\n",
    "            return format(a,'x')\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        alpha = \"0123456789abcdef\"\n",
    "        res = []\n",
    "        for _ in range(8):\n",
    "            res.append(num % 16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "        return ''.join(alpha[i] for i in res[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "MASK = int(\"ffffffff\", 16) + 1\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 0:\n",
    "            num += MASK\n",
    "        ans = []\n",
    "        while num > 0:\n",
    "            i = num % 16\n",
    "            ans.append(str(i) if i < 10 else chr(97 + i - 10))\n",
    "            num //= 16\n",
    "        ans.reverse()\n",
    "        return ''.join(ans)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return \"0\"\n",
    "        \n",
    "        hex_chars = \"0123456789abcdef\"\n",
    "        result = []\n",
    "\n",
    "        # 处理负数的补码表示\n",
    "        if num < 0:\n",
    "            num = 2**32 + num\n",
    "        \n",
    "        while num:\n",
    "            result.append(hex_chars[num % 16])\n",
    "            num = num // 16\n",
    "\n",
    "        return \"\".join(result[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "CONV = \"0123456789abcdef\"\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = []\n",
    "        # 32位2进制数，转换成16进制 -> 4个一组，一共八组\n",
    "        for _ in range(8):\n",
    "            ans.append(num%16)\n",
    "            num //= 16\n",
    "            if not num:\n",
    "                break\n",
    "        return \"\".join(CONV[n] for n in ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = ''\n",
    "        ans2 = [0] * 32\n",
    "        n = 0\n",
    "        m = 0\n",
    "        i = 31\n",
    "        if num == 0: return \"0\"\n",
    "        if num < 0: \n",
    "            # 二进制原码\n",
    "            num = abs(num)\n",
    "            while num:\n",
    "                m = num % 2\n",
    "                ans2[i] = m\n",
    "                i -= 1\n",
    "                num = num // 2\n",
    "            # 反转\n",
    "            for j in range(len(ans2)):\n",
    "                if ans2[j] == 0: ans2[j] = 1\n",
    "                else: ans2[j] = 0\n",
    "            \n",
    "            x = 31\n",
    "            # 末位加1\n",
    "            if ans2[-1] == 1:\n",
    "                while x >= 0:\n",
    "                    ans2[x] = 0\n",
    "                    x -= 1\n",
    "                    if ans2[x] == 0:\n",
    "                        ans2[x] = 1\n",
    "                        break\n",
    "            else:ans2[-1] = 1\n",
    "            # 负数首位为1\n",
    "            ans2[0] = 1\n",
    "            # 2进制转16进制\n",
    "            while len(ans2) > 0:\n",
    "                res = 0\n",
    "                for t in range(4):\n",
    "                    res += ans2[t] * (2 **(4 - t -1))\n",
    "                # 0~9和a~f分开处理\n",
    "                if res < 10:\n",
    "                    ans += str(res)\n",
    "                else:\n",
    "                    ans += chr(ord('a') + (res - 10))\n",
    "                    \n",
    "                ans2 = ans2[4:]\n",
    "                \n",
    "            return ans\n",
    "        else:\n",
    "            while num:\n",
    "                n = num % 16\n",
    "                if n < 10:\n",
    "                    ans += str(n)\n",
    "                else:\n",
    "                    ans += chr(n + 87)\n",
    "                num = num // 16\n",
    "            return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "MASK = 1 << 32\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        if num < 0:\n",
    "            num += MASK\n",
    "        ans = []\n",
    "        while num:\n",
    "            i = num % 16\n",
    "            ans.append(str(i) if i < 10 else chr(97 + i - 10))\n",
    "            num //= 16\n",
    "        ans.reverse()\n",
    "        return ''.join(ans)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    index = \"\".join([str(i) for i in range(10)])+ \"abcdef\"\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num < 0:\n",
    "            return self.toHex(num & 0xffffffff) \n",
    "        if num < 16:\n",
    "            return self.index[num % 16]\n",
    "        return self.toHex(num // 16) + self.index[num % 16]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        s = '0123456789abcdef'\n",
    "        num &= 0xFFFFFFFF\n",
    "        res = ''\n",
    "        while num:\n",
    "            res += s[num % 16]\n",
    "            num >>= 4\n",
    "        \n",
    "        return res[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        ans = []\n",
    "        c16 = '0123456789abcdef'\n",
    "        for i in range(8):\n",
    "            ans.append(num%16)\n",
    "            num//=16\n",
    "            if not num:\n",
    "                break\n",
    "\n",
    "        return ''.join(c16[n] for n in ans[::-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        s = '0123456789abcdef'\n",
    "        res = []\n",
    "        for _ in range(8):\n",
    "            rem = num % 16\n",
    "            res.append(s[rem])\n",
    "            num >>= 4\n",
    "        \n",
    "        return (''. join(res[::-1])).lstrip('0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if num == 0:\n",
    "            return '0'\n",
    "        hexdigits = '0123456789abcdef'\n",
    "        if num < 0:\n",
    "            num += 2 ** 32\n",
    "        ans = ''\n",
    "        while num:\n",
    "            ans += hexdigits[num % 16]\n",
    "            num //= 16\n",
    "        return ans[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        if abs(num) > 16**31:\n",
    "            return False\n",
    "\n",
    "        s = \"0123456789abcdef\"\n",
    "        end_s = \"\"\n",
    "        new_end_s = \"\"\n",
    "        n = abs(num)\n",
    "\n",
    "        while n > 0:\n",
    "                e = n % 16\n",
    "                end_s = end_s + str(s[e])\n",
    "                n = n // 16\n",
    "        end_s = end_s[::-1]\n",
    "        \n",
    "        if num == 0 :\n",
    "            return \"0\"\n",
    "\n",
    "        if num > 0:\n",
    "            return end_s\n",
    "        \n",
    "        if num < 0:\n",
    "            for e in end_s:\n",
    "                new_e = s[15 - s.index(e)]\n",
    "                new_end_s = new_end_s + new_e\n",
    "            \n",
    "        end_s = \"f\" * (8 - len(new_end_s)) + new_end_s\n",
    "         \n",
    "        li = []\n",
    "        for e in end_s:\n",
    "            li.append(e)\n",
    "        \n",
    "\n",
    "        for i in range(len(li) - 1, -1, -1):\n",
    "            if li[i] != \"f\":\n",
    "                li[i] = s[s.index(li[i]) + 1]\n",
    "                break\n",
    "            else:\n",
    "                li[i] = \"0\"\n",
    "        new = \"\"\n",
    "        for e in li:\n",
    "            new = new + e\n",
    "        return new\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "        \n",
    "\n",
    "\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def toHex(self, num: int) -> str:\n",
    "        hash = {10:\"a\", 11:\"b\", 12:\"c\", 13:\"d\", 14:\"e\", 15:\"f\"}\n",
    "        if num == 0:    return \"0\"\n",
    "        if num < 0:\n",
    "            num += 2 ** 32\n",
    "        res = \"\"\n",
    "        while num != 0:\n",
    "            div = num >> 4\n",
    "            r = num - div * 16\n",
    "            res += hash[r] if r >= 10 else str(r)\n",
    "            num = div\n",
    "        return res[::-1]\n",
    "        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
